package com.meshnetics.gb.type.impl.additional;

import com.meshnetics.gb.util.Conversions;
import com.meshnetics.gb.util.Utils;
import com.meshnetics.gb.util.checkers.RangeCheck;
import com.meshnetics.gb.util.checkers.RangeCheckers;
import com.meshnetics.gb.util.formatters.NumberFormatter;
import com.meshnetics.gb.util.formatters.NumberFormatters;

import java.nio.ByteBuffer;

/**
 * Realization of ByteableNumber for u-bye (u-int-8).
 *
 * @author Yury Aserchev (yaserchev@meshnetics.com)
 *         Date: 07.07.2008 20:59:22
 */
public abstract class NumberUByte extends ByteableNumber {
    private static final int SIZE = 1;
    public static final NumberFormatter FORMATTER_UBYTE = NumberFormatters.UBYTE;

    public NumberUByte(final TypeInstantiable type, final String string) {
        super(type, SIZE, Conversions.expandByteToInt((byte) Utils.decodeString(string)));
    }

    public NumberUByte(final TypeInstantiable type, final long value) {
        super(type, SIZE, value);
    }

    public NumberUByte(final TypeInstantiable type, final ByteBuffer buffer) {
        super(type, SIZE, buffer);
    }

    protected final long readBytes(final ByteBuffer buffer) {
        return Conversions.expandByteToInt(buffer.get());
    }

    protected final void writeBytes(final long value, final ByteBuffer buffer) {
        buffer.put((byte) value);
    }

    protected RangeCheck getRangeCheck() {
        return RangeCheckers.UBYTE;
    }

    protected NumberFormatter getFormatter() {
        return FORMATTER_UBYTE;
    }
}
